package com.ruijie.acl.web;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclDeviceAccessGroup;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;

import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.AclDeviceService;
import com.ruijie.acl.service.DeviceAccessGroupService;
import com.ruijie.acl.service.TemplateService;

/**
 * <p>
 * Title: ACL模板管理Action类


 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 为ACL模板管理模块提供页面数据的显示、获取和页面的跳转。


 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time:
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */

@Name("aclTemplateDevcieFromImportAction")
@Scope(ScopeType.CONVERSATION)
public class AclTemplateDevcieFromImportAction extends AclBaseAction {
	@In("#{templateService}")
	private TemplateService templateService;
	@In("#{aclDeviceService}")
	private AclDeviceService aclDeviceService;
	@In("#{templateAction}")
	private TemplateAction templateAction;
	private List<AclDevice> aclDeviceList;
	private AclDevice device;
	private ListDataModel deviceEntityModelList = null;
	@In("#{deviceAccessGroupService}")
	private DeviceAccessGroupService deviceAccessGroupService;
	@In("#{accessGroupService}")
	private AccessGroupService accessGroupService;
	private AclDeviceAccessGroup model;
	private AclDeviceAccessGroup criteria = new AclDeviceAccessGroup();
	private AccessGroup accessGroupSeach = new AccessGroup();
	

	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return this.templateService;
	}

	@Override
	protected String getAddPage() {
		resetModel();
		return "/acl/templateFromDeviceImport.seam";
	}
	
	/**
	 * 取得ACL组分页信息

	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ListDataModel getAccessGroupEntityModelList() {
		adjustPage();
		currentPage = deviceAccessGroupService.pageQueryByAclDeviceGroup(device, accessGroupSeach,page, pageSize);
		deviceEntityModelList = new ListDataModel((List<AclDeviceAccessGroup>) currentPage.getResult());
		return deviceEntityModelList;
	}
	
	@SuppressWarnings("unchecked")
    @Override
    protected void updateModel() {
		deviceEntityModelList = null;
		this.getAccessGroupEntityModelList();
    }
	
	protected Page pagedQuery(EntityObject criteria, int page, int pageSize) {
		return deviceAccessGroupService.pageQueryByAclDeviceGroup(device, accessGroupSeach,page, pageSize);
	}
	

	public void groupSeach(){
		deviceEntityModelList = null;
		updateModel();
		redirectTo("/acl/templateFromDeviceImport.seam");
	}
	/**
	 * 从设备导入acl模板
	 */
	public void deviceFromImport(){
		if (null == getIds() || getIds().length == 0)
            return;
        try {
        	AccessGroup accessGroup = null;
        	List<AccessGroup> accessGroupList = new ArrayList<AccessGroup>();
        	List<String[]> infos=new ArrayList<String[]>();
        	for(long id:getIds()){
        		accessGroup =accessGroupService.get(id);
            	//acl_template_ace_import_info_from_device=导入ACL模板：{0}，成功导入规则{1}条。
        		//去除LIST第一条的空数据
        		if(accessGroup.getAces().size()>0){
        			accessGroup.getAces().remove(null);
        		}
        		int aceCounts=accessGroup.getAces()==null?0:accessGroup.getAces().size();
        		
        		infos.add(new String[]{
        				accessGroup.getName(),
        				new Integer(aceCounts).toString()        				
        		});
        		if(accessGroupList.contains(accessGroup)){
        			throw new BusinessException("acl_template_import_nameExites");
        		}else{
        			accessGroupList.add(accessGroup);
        		}
        	}
        	templateService.loadTemplateFromDevice(accessGroupList);
        	for(String[] msg:infos){
        		this.addGlobalI18nMessage("acl_template_ace_import_info_from_device", msg);
        	}
        	//acl_template_import_access=成功导入{0}个ACL模板
            addGlobalI18nMessage("acl_template_import_access",accessGroupList.size());
        } catch (BusinessException e) {
            // 清除多选框选定记录
            selectedIds.clear();
            throw e;
        } catch(Exception e){
        	selectedIds.clear();
        	throw new BusinessException(e.getMessage());
        }
        updateModel();
        // 如果当前页面已经没有记录(例如删除了当前页面的所有记录)，那么显示上一页

        if (currentPage.getTotalPageCount() < page && page > 1) {
            page--;
            updateModel();
        }
        // 清除多选框选定记录
        selectedIds.clear();
        redirectTo("/acl/templateIndex.seam");
        this.templateAction.setEntityList(null);
	}

	@Override
	public EntityObject getCriteria() {
		return this.criteria;
	}

	@Override
	protected String getDetailPage() {
		return "";
	}

	@Override
	protected String getListPage() {
		return "";
	}

	@Override
	public EntityObject getModel() {
		return this.model;
	}

	@Override
	protected String getUpdatePage() {
		return "";
	}

	@Override
	public void resetModel() {
		model = null;
		model = new AclDeviceAccessGroup();
		criteria = new AclDeviceAccessGroup();
		device = null;
		accessGroupSeach = null;
		accessGroupSeach = new AccessGroup();
	}

	@Override
	public void setCriteria(EntityObject criteria) {
		this.criteria = (AclDeviceAccessGroup) criteria;
	}

	@Override
	public void setModel(EntityObject model) {
		this.model = (AclDeviceAccessGroup) model;
	}
	
	/**
	 * 读取设备列表
	 * 
	 * @return 设备列表
	 */
	public List<SelectItem> getDeviceItems() {
		List<SelectItem> list = new ArrayList<SelectItem>();
		aclDeviceList =aclDeviceService.getAll();
			for (AclDevice entity : aclDeviceList) {
				SelectItem item = new SelectItem(entity, entity.getDevice().getName());
				list.add(item);
			}
		return list;
	}

	public List<AclDevice> getAclDeviceList() {
		return aclDeviceList;
	}

	public void setAclDeviceList(List<AclDevice> aclDeviceList) {
		this.aclDeviceList = aclDeviceList;
	}
	
	public AclDevice getDevice() {
		return device;
	}

	public void setDevice(AclDevice device) {
		this.device = device;
	}

	public ListDataModel getDeviceEntityModelList() {
		return deviceEntityModelList;
	}

	public void setDeviceEntityModelList(ListDataModel deviceEntityModelList) {
		this.deviceEntityModelList = deviceEntityModelList;
	}
	
	public AccessGroup getAccessGroupSeach() {
		return accessGroupSeach;
	}

	public void setAccessGroupSeach(AccessGroup accessGroupSeach) {
		this.accessGroupSeach = accessGroupSeach;
	}


}
